/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package docmanager.engine;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.catalina.Session;

import bf.*;
import docmanager.business.objects.Usuario;
import docmanager.db.*;

/**
 *
 * @author Juan Lucas
 */
public class DMController extends Controller
{
    
	private ConfigData cd = null;
	
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {
        
System.out.println("DMCONTROLLER: Procesando.....");
        
        ConfigData config = ConfigData.getInstance();
//        config.setConfigPath(getServletContext().getResource("/WEB-INF/config"));
        config.setConfigPath(getServletContext().getRealPath(System.getProperty("file.separator") + "WEB-INF" + System.getProperty("file.separator") + "config"));

       // Construimos un DataAccess.
       super.processRequest(request, response);
       
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo()
    {
        return "Short description";
    }// </editor-fold>

    @Override
    protected SecurityHandler getSecurityHandler()
    {
       DMSecurityHandler sh = new DMSecurityHandler();
       return(sh);
    }

    @Override
    protected OperationDispatcher getOperationDispatcher()
    {
        return new DMOperationDispatcher();
    }

    @Override
    protected String getDefaultURL()
    {
        return("login.jsp");
    }

    @Override
    protected String getNoPermissionURL()
    {
        return("login.jsp?error=1");
    }

    @Override
    // Cambiara la pagina segun el usuario cargado.
    protected String getMainURL()
    {
        return("Main.jsp");
    }
    
    public static String getServerPath(HttpServletRequest request)
    {
        String s= "http://" + request.getServerName()+":"+request.getServerPort();
        s = s + "/DocManager2";
        return s;
    }

	@Override
	protected String getLogPath() {
		if (this.cd == null)
			this.cd = ConfigData.getInstance();
		return cd.getLogPath();
	}

	@Override
	protected void cargarUsuario(String login, HttpSession session) throws ServletException 
	{
		try {
			Connection conn = ConnectionPool.getConnection();
			Usuario u = new Usuario(conn, login);
			u.cargarRoles();
			session.setAttribute("usuarioObjeto", u);
			ConnectionPool.closeConn(conn);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new ServletException();
		}
		

	}
    
}
